// #pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int N=205;
int a[N];
int b[N];
int f[N][N][N];
int solve(int l,int r,int len)
{
	int all=b[r]+len;
	if(l==r) return all*all;
	if(~f[l][r][len]) return f[l][r][len];
	int res=solve(l,r-1,0)+all*all;
	for(int k=r-1;k>=l;k--)
	{
		if(a[k]==a[r]) res=max(res,solve(l,k,all)+solve(k+1,r-1,0));
	}
	return f[l][r][len]=res;
} 
int main()
{
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++) cin>>b[i];
	
	memset(f,-1,sizeof(f));
	printf("%d",solve(1,n,0));

	return 0;
}
